package com.et.util;

import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeansException;
import org.springframework.beans.FatalBeanException;
import org.springframework.util.Assert;


public abstract class BeanUtils extends org.springframework.beans.BeanUtils {
	private final static List<String> notCopyType=new ArrayList<String>();
	static{
		notCopyType.add("int");
		notCopyType.add("long");
	}
    /**
     * 对象拷贝操作-如果源对象中包含空的属性，该属性将不会进行拷贝
     * @param source 源对象    不为空的对象
     * @param target 目标对象  不为空的对象
     * @throws BeansException
     */
    public static void updateCopyProperties(Object source, Object target) throws BeansException {  
      Assert.notNull(source, "Source must not be null");  
      Assert.notNull(target, "Target must not be null");  
      Class<?> actualEditable = target.getClass();  
      PropertyDescriptor[] targetPds = getPropertyDescriptors(actualEditable);
      String targetPropertyType="";
      for (PropertyDescriptor targetPd : targetPds) { 
    	  targetPropertyType=targetPd.getPropertyType().toString();
        if (targetPd.getWriteMethod() != null) {  
          PropertyDescriptor sourcePd = getPropertyDescriptor(source.getClass(), targetPd.getName());  
          if (sourcePd != null && sourcePd.getReadMethod() != null) {  
            try {  
              Method readMethod = sourcePd.getReadMethod();  
              if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) {  
                readMethod.setAccessible(true);  
              }  
              Object value = readMethod.invoke(source);  
              // 这里判断以下value是否为空 当然这里也能进行一些特殊要求的处理 例如绑定时格式转换等等  
              if (value != null) {
            	if(notCopyType.contains(targetPropertyType)){
            		if((Long.valueOf(value.toString()))==0){
            			continue;
            		}
            	};
                Method writeMethod = targetPd.getWriteMethod();  
                if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) {  
                  writeMethod.setAccessible(true);  
                }  
                writeMethod.invoke(target, value);  
              }  
            } catch (Throwable ex) {  
              throw new FatalBeanException("Could not copy properties from source to target", ex);  
            }  
          }  
        }  
      }  
    }  
  }  